using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for NHAPXS.
	/// </summary>
	public class NHAPXS
	{   		
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		string connStr = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
		public NHAPXS(SMS_MO MO)
		{
			WMO = MO;
		}

		public NHAPXS()
		{
		}
		#endregion
		public void InsertResultDBTT(string msg)
		{
			string temp="";	
			string flag="";
			string gdb="", g1="", g2="", g3="", g4="", g5="", g6="", g7="";
			string strSQLSelect = "SELECT * FROM XSTemp WHERE (datepart(dd,ngay) = datepart(dd,getdate())) AND (datepart(mm,ngay) = datepart(mm,getdate()))";
			SqlConnection conn = new SqlConnection(connStr);
			conn.Open();
			SqlDataAdapter da = new SqlDataAdapter(strSQLSelect, conn);
			DataSet ds = new DataSet();
			da.Fill(ds);
			conn.Close();
			if (ds.Tables[0].Rows.Count==0)
			{
				#region Insert Record.Empty;
				string strSQLInsert = "INSERT INTO XSTemp(Tentinh, Matinh, GiaiDacBiet, GiaiNhat, GiaiNhi, GiaiBa, GiaiTu, GiaiNam, GiaiSau, GiaiBay, GiaiTam, Ngay, Mamien, Tenmien)";
				strSQLInsert += " VALUES(@Tentinh, @Matinh, @GiaiDacBiet, @GiaiNhat, @GiaiNhi, @GiaiBa, @GiaiTu, @GiaiNam, @GiaiSau, @GiaiBay, @GiaiTam, @Ngay, @Mamien, @Tenmien)";
				conn.Open();
				SqlCommand _cmd;
				_cmd = new SqlCommand(strSQLInsert, conn);					
				_cmd.Parameters.Clear();
				_cmd.Parameters.Add("@Tentinh","");
				_cmd.Parameters.Add("@Matinh", "");
				_cmd.Parameters.Add("@GiaiDacBiet",gdb);
				_cmd.Parameters.Add("@GiaiNhat",g1);
				_cmd.Parameters.Add("@GiaiNhi",g2);
				_cmd.Parameters.Add("@GiaiBa",g3);
				_cmd.Parameters.Add("@GiaiTu",g4);
				_cmd.Parameters.Add("@GiaiNam",g5);
				_cmd.Parameters.Add("@GiaiSau",g6);
				_cmd.Parameters.Add("@GiaiBay",g7);
				_cmd.Parameters.Add("@GiaiTam","1");
				_cmd.Parameters.Add("@Ngay",DateTime.Now);
				_cmd.Parameters.Add("@Mamien","MB");
				_cmd.Parameters.Add("@Tenmien","");	
				_cmd.ExecuteNonQuery();
				conn.Close();
				#endregion	
			}				
			if (WMO.Message.Length<=20)
			{
				#region Statement 1
				flag = "Statement 1";
				//HP 19/09\nDB:\n1:89681
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=34)
			{
				#region Statement 2
				//HP 19/09\nDB:\n1:89681\n2:00926-08912
				flag = "Statement 2";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=72)
			{
				#region Statement 3
				//HP 19/09\nDB:\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441
				flag = "Statement 3";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=94)
			{
				#region Statement 4
				//HP 19/09\nDB:\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441\n4:0495-2657-9869-2025
				flag = "Statement 4";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				g4 = s[5].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=126)
			{
				#region Statement 5
				//HP 19/09\nDB:\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441\n4:0495-2657-9869-2025\n5:6552-1639-3206-2518-9476-7845
				flag = "Statement 5";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				g4 = s[5].Substring(2);
				g5 = s[6].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=140)
			{
				#region Statement 6
				//HP 19/09\nDB:\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441\n4:0495-2657-9869-2025\n5:6552-1639-3206-2518-9476-7845\n6:167-632-389
				flag = "Statement 6";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				g4 = s[5].Substring(2);
				g5 = s[6].Substring(2);
				g6 = s[7].Substring(2);
				#endregion
			}
			else if (WMO.Message.Length<=154)
			{
				#region Statement 7
				//HP 19/09\nDB:\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441\n4:0495-2657-9869-2025\n5:6552-1639-3206-2518-9476-7845\n6:167-632-389\n7:27-37-29-45
				flag = "Statement 7";
				string[] s = msg.Split('\n');
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				g4 = s[5].Substring(2);
				g5 = s[6].Substring(2);
				g6 = s[7].Substring(2);
				g7 = s[8].Substring(2);
				#endregion
			}
			else 
			{
				#region Statement 8
				//HP 19/09\nDB:96996\n1:89681\n2:00926-08912\n3:61102-20736-42533-50042-08395-66441\n4:0495-2657-9869-2025\n5:6552-1639-3206-2518-9476-7845\n6:167-632-389\n7:27-37-29-45
				flag = "Last Statement"; 
				string[] s = msg.Split('\n');
				gdb = s[1].Substring(3);
				g1 = s[2].Substring(2);
				g2 = s[3].Substring(2);
				g3 = s[4].Substring(2);
				g4 = s[5].Substring(2);
				g5 = s[6].Substring(2);
				g6 = s[7].Substring(2);
				g7 = s[8].Substring(2);
				#endregion
			}
			string strSQLUpdate = "UPDATE XSTemp SET Tentinh = @Tentinh, Matinh = @Matinh, GiaiDacBiet = @GiaiDacBiet, GiaiNhat = @GiaiNhat, GiaiNhi = @GiaiNhi, GiaiBa = @GiaiBa, GiaiTu = @GiaiTu, GiaiNam = @GiaiNam, GiaiSau = @GiaiSau, GiaiBay = @GiaiBay, GiaiTam = @GiaiTam, Ngay = @Ngay, Mamien = @Mamien, Tenmien = @Tenmien WHERE (datepart(dd,ngay) = datepart(dd,getdate())) AND (datepart(mm,ngay) = datepart(mm,getdate()))";
			conn.Open();
			SqlCommand cmd;
			cmd = new SqlCommand(strSQLUpdate, conn);					
			cmd.Parameters.Clear();
			cmd.Parameters.Add("@Tentinh","");
			cmd.Parameters.Add("@Matinh", "");
			cmd.Parameters.Add("@GiaiDacBiet",gdb);
			cmd.Parameters.Add("@GiaiNhat",g1);
			cmd.Parameters.Add("@GiaiNhi",g2);
			cmd.Parameters.Add("@GiaiBa",g3);
			cmd.Parameters.Add("@GiaiTu",g4);
			cmd.Parameters.Add("@GiaiNam",g5);
			cmd.Parameters.Add("@GiaiSau",g6);
			cmd.Parameters.Add("@GiaiBay",g7);
			cmd.Parameters.Add("@GiaiTam","");
			cmd.Parameters.Add("@Ngay",DateTime.Now);
			cmd.Parameters.Add("@Mamien","MB");
			cmd.Parameters.Add("@Tenmien","");	
			cmd.ExecuteNonQuery();
			conn.Close();
			temp = "Thank you for entering --" + flag + "-- to database";
			WMT = new SMS_MT(WMO);
			WMT.AddMessage(WMO.UserID, temp, SMS_ContentType.SMS_TEXT);
			WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
			WMT.Insert();
		}
	}
}
				
	